package com.lenovo.manufacture.module;

import android.graphics.Color;
import android.os.Build;
import android.os.Handler;

import androidx.annotation.RequiresApi;

import com.beardedhen.androidbootstrap.BootstrapLabel;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.lenovo.manufacture.BaseActivity;
import com.lenovo.manufacture.R;
import com.lenovo.manufacture.annotation.Layout;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import butterknife.BindView;

public class Module13 extends BaseActivity {

    @BindView(R.id.chart)
    LineChart chart;
    List<Entry> entryList1;
    List<Entry> entryList;

    Handler handler = new Handler();
    Runnable runnable;
    @BindView(R.id.bl_1)
    BootstrapLabel bl1;
    @BindView(R.id.bl_2)
    BootstrapLabel bl2;

    @Layout(value = R.layout.activity_module13,title = "温度显示")
    protected void init() {
        entryList = new ArrayList<>();
        entryList1 = new ArrayList<>();
        Description description = new Description();
        description.setText("温度℃");
        description.setTextSize(30);
        chart.setDescription(description);
        chart.setScaleEnabled(false);
        chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
//        chart.getXAxis().setAxisMinimum(0);
//        chart.getXAxis().setAxisMaximum(24);
        chart.getAxisLeft().setAxisMinimum(-10);
        chart.getAxisLeft().setAxisMaximum(40);
        chart.getXAxis().setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                Date date = new Date(1000 * ((long) value));
                return (date.getHours()) + ":00";
            }
        });
        update();
    }


    private void update() {
        json.put("id", 1);
        runnable = new Runnable() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void run() {
                async(apiService.getInfo(json), data -> {
                    Map<String, String> stringMap = data.getData().get(0);
                    String outdoorTemp = stringMap.get("outdoorTemp");
                    String workshopTemp = stringMap.get("workshopTemp");
                    bl1.setText("环境温度\n" + outdoorTemp + "℃");
                    bl2.setText("车间温度\n" + workshopTemp + "℃");
//                    Date date = new Date(1000 * Long.valueOf(stringMap.get("time")));
//                    float time = date.getHours() + ((float) date.getMinutes()) / 100;
                    System.out.println(stringMap.get("time"));
                    Entry entry = new Entry(Float.parseFloat(stringMap.get("time")), Float.parseFloat(outdoorTemp));
                    Entry entry1 = new Entry(Float.parseFloat(stringMap.get("time")), Float.parseFloat(workshopTemp));
                    entryList.add(entry);
                    entryList1.add(entry1);

                    setData();

                    ILineDataSet dataSetByIndex = chart.getLineData().getDataSetByIndex(0);
                    if (dataSetByIndex.getEntryCount() > 24) dataSetByIndex.removeFirst();

                    ILineDataSet dataSetByIndex2 = chart.getLineData().getDataSetByIndex(1);
                    if (dataSetByIndex2.getEntryCount() > 24) dataSetByIndex2.removeFirst();

                    chart.invalidate();
                    handler.postDelayed(this, 2000);
                });
            }
        };
        handler.postDelayed(runnable, 0);
    }

    private void setData() {
        LineDataSet dataSet = new LineDataSet(entryList, "环境温度");
        dataSet.setColor(Color.parseColor("#ff5b5b"));
        dataSet.setLineWidth(10);

        LineDataSet lineDataSet = new LineDataSet(entryList1, "车间温度");
        lineDataSet.setColor(Color.parseColor("#82aaff"));
        lineDataSet.setLineWidth(10);

        LineData lineData = new LineData(dataSet, lineDataSet);
        chart.setData(lineData);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
    }
}